/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.sd.web;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sd.common.constants.RecvConstant;
import com.jeesite.modules.sd.common.constants.RecvUppUnitEnum;
import com.jeesite.modules.sd.entity.DocRecv;
import com.jeesite.modules.sd.entity.UppUnit;
import com.jeesite.modules.sd.service.DocRecvService;
import com.jeesite.modules.sd.service.UppUnitService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 上级来文单位Controller
 *
 * @author zhanchj
 * @version 2020-07-01
 */
@Controller
@RequestMapping(value = "${adminPath}/sd/uppUnit")
public class UppUnitController extends BaseController {

	@Autowired
	private UppUnitService uppUnitService;

	@Autowired
	private DocRecvService docRecvService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public UppUnit get(String id, boolean isNewRecord) {
		return uppUnitService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("sd:uppUnit:view")
	@RequestMapping(value = {"index", ""})
	public String index(UppUnit uppUnit, Model model) {
		model.addAttribute("uppUnit", uppUnit);
		return "modules/sd/uppUnitIndex";
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("sd:uppUnit:view")
	@RequestMapping(value = {"list", ""})
	public String list(UppUnit uppUnit, Model model) {
		model.addAttribute("uppUnit", uppUnit);
		return "modules/sd/uppUnitlist";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("sd:uppUnit:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<UppUnit> listData(UppUnit uppUnit, HttpServletRequest request, HttpServletResponse response) {
		uppUnit.setPage(new Page<>(request, response));
		return uppUnitService.findPage(uppUnit);
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("sd:uppUnit:view")
	@RequestMapping(value = "form")
	public String form(UppUnit uppUnit, Model model) {
		model.addAttribute("uppUnit", uppUnit);
		return "modules/sd/uppUnitForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("sd:uppUnit:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated UppUnit uppUnit) {
		uppUnitService.save(uppUnit);
		return renderResult(Global.TRUE, text("保存来文单位成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("sd:uppUnit:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(UppUnit uppUnit) {
		uppUnit.setStatus(UppUnit.STATUS_DISABLE);
		uppUnitService.updateStatus(uppUnit);
		return renderResult(Global.TRUE, text("停用来文单位成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("sd:uppUnit:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(UppUnit uppUnit) {
		uppUnit.setStatus(UppUnit.STATUS_NORMAL);
		uppUnitService.updateStatus(uppUnit);
		return renderResult(Global.TRUE, text("启用来文单位成功"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("sd:uppUnit:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(UppUnit uppUnit) {
		DocRecv docRecv = new DocRecv();
		docRecv.setUnitCode(uppUnit.getId());
		long count = docRecvService.findCount(docRecv);
		if (count > 0) {
			return renderResult(Global.FALSE, text("该单位已有文件存在无法删除！"));
		}

		uppUnitService.delete(uppUnit);
		return renderResult(Global.TRUE, text("删除来文单位成功！"));
	}


	/**
	 *
	 * @param isShowCode 是否显示编码（true or 1：显示在左侧；2：显示在右侧；false or null：不显示）
	 * @param isTop 是否只显示顶级节点
	 * @return
	 */
	@RequiresPermissions("sd:uppUnit:view")
	@RequestMapping(value = "treeData")
	@ResponseBody
	public List<Map<String, Object>> treeData(String isShowCode,@RequestParam(value = "isTop",defaultValue = "false") Boolean isTop) {
		List<Map<String, Object>> mapList = ListUtils.newArrayList();
		List<UppUnit> list;
		if (isTop) {
			list = new ArrayList<>();
		} else {
			list = uppUnitService.findList(new UppUnit());

		}
		//单位类型（1保密局,2公安部,3其它单位）
		for (int i = 1; i < 4; i++) {

			UppUnit unit = new UppUnit();
			unit.setUnitType(i+"");
			long count = uppUnitService.findCount(unit);
			//判断该类型是否有下级单位
			if (count == 0) {
				continue;
			}
			unit.setId(i + "");
			if (i == Integer.parseInt(RecvUppUnitEnum.SECRET.getKey())) {
				unit.setUnitName(RecvUppUnitEnum.SECRET.getValue());
			} else if (i == Integer.parseInt(RecvUppUnitEnum.MOPS.getKey())) {
				unit.setUnitName(RecvUppUnitEnum.MOPS.getValue());
			} else {
				unit.setUnitName(RecvUppUnitEnum.OTHER.getValue());
			}
			unit.setUnitType(RecvConstant.TOP_UNIT);

			list.add(unit);
		}
		for (UppUnit e : list) {
			Map<String, Object> map = MapUtils.newHashMap();
			map.put("id", e.getId());
			map.put("pId", e.getUnitType());
			map.put("name", StringUtils.getTreeNodeName(isShowCode, e.getId(), e.getUnitName()));
			map.put("title", e.getUnitName());
			mapList.add(map);
		}
		return mapList;
	}


}